package com.cy.yonghe.contorller;

import com.cy.yonghe.model.Door;
import com.cy.yonghe.service.IDoorService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;


import java.util.List;

@Controller
@RequestMapping("/door")
public class DoorController {

    @Autowired
    IDoorService doorService;

    //url:http://localhost:8080/door/list
   @RequestMapping("/list")
    public String list(Model model,String name,Integer pageNum){
       List<Door> doors = doorService.list(pageNum,name);
       //将分页查询的结果封装为PageInfo对象
       PageInfo<Door> pageInfo = new PageInfo<>(doors);
       model.addAttribute("pageInfo",pageInfo);
       return "door/list";
    }

    //转发到新增门店页面
    @GetMapping("/add")
    public String add(){
     return "door/add";
   }

   //新增门店处理
   @PostMapping("/add")
   public String add(Door door){
       doorService.save(door);
       return "redirect:/door/list";
   }

   //转发到修改门店页面，/door/update?id=1
    @GetMapping("/update")
    /*@ResponseBody*/
    public String update(Integer id,Model model){
        Door door = doorService.getById(id);
        model.addAttribute("door",door);
        return "door/update";
    }
    //修改门店处理
    @PostMapping("/update")
    public String update(Door door){
        doorService.update(door);
        return "redirect:/door/list";
    }

    //删除门店处理
    @GetMapping("/delete")
    public String delete(Integer id){
        doorService.delete(id);
        return "redirect:/door/list";
    }
}
